<?php
    class CustomerCareController extends CpController
    {
        public function init(){
            $url = new Url();
            $this->breadcrumbs[] = array('name'=>'Chăm sóc','link'=>$url->createUrl("game/index"),'class'=>'');
        }
        public function actionIndex(){
            $row_per_page = 20;
            $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"01-".date('m-Y');
            $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):date('d-m-Y');
            $username=isset($_GET['username']) ? mysql_escape_string($_GET['username']):"";
            $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
            list($max_page,$count,$data)=ACustomerCare::getFeedbackSearch($from_date,$to_date,$username,$page,$row_per_page); 

            $this->render("index",
            array(
            "from_date"=>$from_date,
            "to_date"=>$to_date,
            "username"=>$username,
            "data"=> $data,
            "max_page"=>$max_page,
            "count"=>$count ,
            "page"=>$page
            )
            );
        }
        public function actionAjaxReply(){
            $id=$_POST['id'];
            $dataUser=ACustomerCare::getUserId($id);
            $title_msg="Trả lời góp ý - mtop.vn";
            $description=$_POST['mes'];
            $user_from='admin'; 
            $user_from_id=17;
            $user_to_id=$dataUser['id'];
            $user_to=$dataUser['username'];
            $is_read=array($user_to_id=>0);
            $is_read=json_encode($is_read);
            $status=array($user_to_id=>1);
            $status=json_encode($status);
            $from_admin=1;
            $create_date=time();
            $data = array(
                "user_from"=>array("value"=>$user_from,"type"=>2),
                "user_from_id"=>array("value"=>$user_from_id,"type"=>1),
                "user_to"=>array("value"=>$user_to,"type"=>2),
                "user_to_id"=>array("value"=>$user_to_id,"type"=>1),
                "from_admin"=>array("value"=>$from_admin,"type"=>1),
                "status"=>array("value"=>$status,"type"=>2),
                "is_read"=>array("value"=>$is_read,"type"=>2),
                "title"=>array("value"=>$title_msg,"type"=>2),
                "content"=>array("value"=>$description,"type"=>2),
                "create_date"=>array("value"=>$create_date,"type"=>1),
                "create_user"=>array("value"=>$user_from,"type"=>2),
            );
            $table = "c_msg_user";
            $msg_id= CommonDB::insertRow($table,$data);
            if($msg_id>0){
                echo 1;
            } else {
                echo "Chưa gửi được tin nhắn";
            }
            /*Gửi Mail*/
            /* $data["email"] =$dataUser['email'];
            if(Yii::app()->params["sendEmail"]){
            $msg = $_POST['mes'];
            Yii::import("application.vendors.PhpMailer",true);
            $mail = new PHPMailer(true);
            $mail->IsSMTP();
            $mailer = new Mailer($mail);

            $mailer->setContent("Trả lời góp ý - mtop.vn", $msg);
            $mailer->sendMail($data["email"]);
            echo 1;
            } else{
            echo "Chưa gửi được email";
            }*/
        }
        public function actionLogsms(){
            $row_per_page = 20;
            $game=array();
            $username=array();
            $partner=array();
            $rate_wap=array();
            $money_wap=array();
            $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):date('d-m-Y');
            $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):date('d-m-Y');
            $name_search= isset($_GET["name_search"]) ? mysql_escape_string($_GET["name_search"]):"";
            $search_type = isset($_GET["search_type"]) ? intval($_GET["search_type"]):2;
            $content_type = isset($_GET["content_type"]) ? intval($_GET["content_type"]):1;
            $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
            $rate = APaymentRevenue::getAllRate();
            $rate_sms = APaymentRevenue::getAllRateSms();
            list($max_page,$count,$data)=ACustomerCare::getDataSmsLogSearch($from_date,$to_date,$search_type,$page,$row_per_page,$content_type,$name_search);
            foreach($data as $key=>$value){
                $index_rate = $value["month"] . "_" . $value["year"];
                $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $value["serviceNumber"]);
                $rate_system=$rate_sms[$index_rate][$sc]/100;
                if($value['type']==1){
                    $rate_wap[$key]=$value['percent_wapmaster'];
                    $game[$key]=ACustomerCare::getGameById($value['dataId']);
                    $money_wap[$key]=$value['price']*0.85*($rate_wap[$key]/100)*$rate_system;
                }else{
                    $rate_wap[$key]=$rate[$index_rate][$value['type']]['percent_wapmaster']; 
                }
                $partner[$key]=ACustomerCare::getPartnerById($value['partnerId']);
                $username[$key]=ACustomerCare::getUserId($value['userId']);
                $money_wap[$key]=$value['price']*0.85*($rate_wap[$key]/100)*$rate_system;
            }
            $this->render("logsms",
            array(
            "from_date"=>$from_date,
            "to_date"=>$to_date,
            "name_search"=>$name_search,
            "search_type"=>$search_type,
            "content_type"=>$content_type,
            "max_page"=>$max_page,
            "count"=>$count,
            "data"=>$data,
            "page"=>$page,
            "game"=>$game,
            "partner"=>$partner,
            "username"=>$username,
            "rate_wap"=>$rate_wap,
            "money_wap"=>$money_wap
            )
            );
        }
        public function actionLogcard(){
            $row_per_page = 20;
            $game=array();
            $username=array();
            $partner=array();
            $rate_wap=array();
            $money_wap=array();
            $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):date('d-m-Y');
            $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):date('d-m-Y');
            $keyword = isset($_GET["keyword"]) ? mysql_escape_string($_GET["keyword"]):"";
            $name_search= isset($_GET["name_search"]) ? mysql_escape_string($_GET["name_search"]):"";
            $search_type = isset($_GET["search_type"]) ? intval($_GET["search_type"]):5;
            $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
            $rate = APaymentRevenue::getAllRate();
            $rate_sms = APaymentRevenue::getAllRateSms();
            //var_dump($rate_sms);die;
            //var_dump($rate);die;
            list($max_page,$count,$data)=ACustomerCare::getDataCardLogSearch($from_date,$to_date,$keyword,$search_type,$page,$row_per_page,$name_search);
            foreach($data as $key=>$value){
                $game[$key]=ACustomerCare::getGameById($value['dataId']);
                $partner[$key]=ACustomerCare::getPartnerById($value['partnerId']);
                $username[$key]=ACustomerCare::getUserId($value['userId']);
                $money_wap[$key]=$value['price']*0.85*($value['percent_wapmaster']/100);
            }
            //var_dump($username);die;
            //var_dump($data);die;
            $this->render("logcard",
            array(
            "from_date"=>$from_date,
            "to_date"=>$to_date,
            "keyword"=>$keyword,
            "name_search"=>$name_search,
            "search_type"=>$search_type,
            "max_page"=>$max_page,
            "count"=>$count,
            "data"=>$data,
            "page"=>$page,
            "game"=>$game,
            "partner"=>$partner,
            "username"=>$username,
            "money_wap"=>$money_wap
            )
            );
        }
    }
?>
